//
// Created by xili on 2024/8/2 19:52.
// Go big or go home.
//
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int longestWPI(vector<int> &hours) {
        unordered_map<int, int> ump;
        ump[0] = -1;
        int n = hours.size();
        int ans = 0, sum = 0;
        for (int i = 0; i < n; i++) {
            sum += hours[i] > 8 ? 1 : -1;
            if (sum > 0) {
                ans = i + 1;
            } else {
                if (ump.count(sum - 1)) {
                    ans = max(ans, i - ump[sum - 1]);
                }
            }
            if (!ump.count(sum)) {
                ump[sum] = i;
            }
        }
        return ans;
    }
};